import json
import random
import time
import requests
import uuid

from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.db import transaction
from datetime import datetime
from django.http import JsonResponse
from django.forms import model_to_dict
from django.views.decorators.http import require_http_methods
from pyexpat.errors import messages
from rest_framework.decorators import api_view, action
from rest_framework.views import APIView
from rest_framework import serializers
from drf_spectacular.utils import extend_schema, extend_schema_view, OpenApiParameter

from App.view.poster.view import PosterSerializer
from models.models import WeChatUser as ChatUser, Product, WeChatPayOrder as Order, InvitationRecord
from models.models import WeChatUser
from tool.permissions import IsTokenValid
from tool.tools import getEnvConfig, logger, res, CustomStatus
from tool.token_tools import CustomTokenTool, _redis
from tool.tool_wechat import getWechatAccessToken, checkWechatUserIsExist, genterateWechatAuth, decryptData, \
    updataOrderNotify, generateSign
from tool.utils import CustomPagination, ApiResponse
from tool.base_views import BaseViewSet
from django.utils.translation import gettext as _
is_token = IsTokenValid()


@api_view(['POST'])
def getDouyinOpenId(request):
    """
    微信登陆byOpenId
    :param request:
    :return:
    """
    res.clear()
    try:
        if request.method == 'POST':
            body = json.loads(request.body)
            code = body.get('code')
            anonymous_code = body.get('anonymous_code')
            if not code:
                return JsonResponse(CustomStatus.SUCCESS)
                # return ApiResponse(code=200,message="登录成功")
                # return JsonResponse(CustomStatus.WECHAT_CODE_INVALID.to_response())
            base_url = getEnvConfig("WECHAT_BASE_URL")
            end_path = getEnvConfig("LOGIN_END_PATH")
            appId = getEnvConfig("APPID")

    except Exception as e:
        logger.error(f"获取微信openId失败: {str(e)}")
        return JsonResponse(CustomStatus.WECHAT_OPENID_ERROR.to_response())


